package org.lanqiao.webfm.dao;

import java.io.Serializable;
import java.sql.SQLException;

import javacommon.base.BaseJdbcDao;
import javacommon.ijdbc.RowMapper;
import javacommon.util.StringUtils;
import javacommon.web.page.Page;
import javacommon.web.page.PageRequest;

import org.lanqiao.webfm.model.Orders;
import org.lanqiao.webfm.model.OrdersMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*@Author zhengwei
**/
public class OrdersDao extends BaseJdbcDao<Orders>{
	private static final Logger LOGGER = LoggerFactory.getLogger(OrdersDao.class);
	private OrdersDao(){}
	private static OrdersDao instance = new OrdersDao();
	public static OrdersDao getInstance(){
		return instance;
	}
	public Class getEntityClass() {
		return Orders.class;
	}
	
	/*public RowMapper<Orders> getMapper() {
		return new OrdersMapper();
	}*/
	public String getIdColumnName() {
		return "ID";
	}
	public String getTableName() {
		return "SP_ORDERS";
	}
	
	public Orders getById(Serializable id) throws SQLException {
		String sql = "select * from SP_ORDERS t1 "
				+ "where t1.ID=?";
        return operation.queryForUniqueBean(sql, getEntityClass(), id);
	}
	@Override
	public void save(Orders entity) throws SQLException {
		StringBuilder sql = new StringBuilder("insert into SP_ORDERS")
				.append(" (")
				.append("ID").append(",")
				.append("USER_ID").append(",")
				.append("EDATE").append(",")
				.append("ADDRESS").append(",")
				.append("CONTACTMAN").append(",")
				.append("PHONENUMBER").append(",")
				.append("POSTCODE").append(",")
				.append("TOTAL").append(",")
				.append("DEALDATE").append(",")
				.append("STATUS").append(",")
				.append("NOTE")
				.append(") ")
				.append("values(dev_seq.nextval,")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?")
				.append(")");
			
		super.operation
				.execute(sql.toString(), 
								entity.getUserId(),
								entity.getEdate(),
								entity.getAddress(),
								entity.getContactman(),
								entity.getPhonenumber(),
								entity.getPostcode(),
								entity.getTotal(),
								entity.getDealdate(),
								entity.getStatus(),
								entity.getNote()
				);
	}
	@Override
	public void update(Orders entity) throws SQLException {
		StringBuilder sql = new StringBuilder("update SP_ORDERS")
				.append(" set ") 
				.append("USER_ID=?").append(",")
				.append("EDATE=?").append(",")
				.append("ADDRESS=?").append(",")
				.append("CONTACTMAN=?").append(",")
				.append("PHONENUMBER=?").append(",")
				.append("POSTCODE=?").append(",")
				.append("TOTAL=?").append(",")
				.append("DEALDATE=?").append(",")
				.append("STATUS=?").append(",")
				.append("NOTE=?")
				.append(" where ID=?");
		super.operation
				.execute(sql.toString(),
							entity.getUserId(),
							entity.getEdate(),
							entity.getAddress(),
							entity.getContactman(),
							entity.getPhonenumber(),
							entity.getPostcode(),
							entity.getTotal(),
							entity.getDealdate(),
							entity.getStatus(),
							entity.getNote(),
						entity.getId());
	}
	/**
	 * 根据query对象来查询数据，结果取决于query中有查询条件及query中的分页信息
	 */
	public Page<Orders> findPage(PageRequest<Orders> query) throws SQLException{
		Orders orders = (Orders) query;
        StringBuilder sql = new StringBuilder("select t.* from SP_ORDERS t where 1=1 ");
        if(StringUtils.hasLength(orders.getId())) {
         	sql.append(" and  t.ID =  ").append(orders.getId());
        }
        if(StringUtils.hasLength(orders.getUserId())) {
         	sql.append(" and  t.USER_ID =  ").append(orders.getUserId());
        }
        if(StringUtils.hasLength(orders.getEdateBegin())) {
            sql.append(" and  t.edate >= ").append(orders.getEdateBegin() );
        }
        if(StringUtils.hasLength(orders.getEdateEnd())) {
            sql.append(" and  t.edate <= ").append(orders.getEdateEnd());
        }
        if(StringUtils.hasLength(orders.getAddress())) {
            sql.append(" and  t.ADDRESS =  '").append(orders.getAddress()).append("'");
        }
        if(StringUtils.hasLength(orders.getContactman())) {
            sql.append(" and  t.CONTACTMAN =  '").append(orders.getContactman()).append("'");
        }
        if(StringUtils.hasLength(orders.getPhonenumber())) {
            sql.append(" and  t.PHONENUMBER =  '").append(orders.getPhonenumber()).append("'");
        }
        if(StringUtils.hasLength(orders.getPostcode())) {
            sql.append(" and  t.POSTCODE =  '").append(orders.getPostcode()).append("'");
        }
        if(StringUtils.hasLength(orders.getTotal())) {
         	sql.append(" and  t.TOTAL =  ").append(orders.getTotal());
        }
        if(StringUtils.hasLength(orders.getDealdateBegin())) {
            sql.append(" and  t.dealdate >= ").append(orders.getDealdateBegin() );
        }
        if(StringUtils.hasLength(orders.getDealdateEnd())) {
            sql.append(" and  t.dealdate <= ").append(orders.getDealdateEnd());
        }
        if(StringUtils.hasLength(orders.getStatus())) {
            sql.append(" and  t.STATUS =  '").append(orders.getStatus()).append("'");
        }
        if(StringUtils.hasLength(orders.getNote())) {
            sql.append(" and  t.NOTE =  '").append(orders.getNote()).append("'");
        }
        if(StringUtils.hasLength(orders.getSortColumns())) {
            sql.append(" order by ").append(orders.getSortColumns());
        }	
        
		return pageQuery(sql.toString(),orders);
	}
	
	public void deleteByIds(String[] ids) throws SQLException {
		String[] sqls = new String[ids.length];
		for (int i = 0; i < sqls.length; i++) {
			sqls[i] = "delete from SP_ORDERS where ID=" + ids[i];
		}
		operation.executeBatch(sqls);
	}

}
